package com.txzt.gaea.business.device.service.impl;

import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.domain.model.DevicePurchase;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.business.device.service.IDevicePurchaseService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.PurchaseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class DevicePurchaseServiceImpl extends BaseServiceImpl<DevicePurchase> implements IDevicePurchaseService {

    @Autowired
    private IDeviceInfoService deviceInfoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int finishDevicePurchase(Long id) {
        DevicePurchase resPurchase = sqlManager.single(DevicePurchase.class,id);
        //更新库存信息
        DeviceInfo resDevice = deviceInfoService.selectById(resPurchase.getDeviceId());
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setId(resPurchase.getDeviceId());
        deviceInfo.setInventoryQty(resDevice.getInventoryQty() + resPurchase.getPurchaseQty());
        deviceInfo.setAvailableQty(resDevice.getAvailableQty() + resPurchase.getPurchaseQty());
        deviceInfoService.updateById(deviceInfo);
        //更新设备采购状态
        DevicePurchase devicePurchase = new DevicePurchase();
        devicePurchase.setId(id);
        devicePurchase.setPurchaseStatus(PurchaseStatus.PURCHASED.getCode());
        return sqlManager.updateTemplateById(devicePurchase);
    }
}
